package com.umpay.business.consume.impl;


import com.umpay.business.consume.ConsumeFileUploadNotifyBusiness;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.req.consume.ConsumeFileUploadNotifyReq;
import com.umpay.dto.merAccess.res.consume.ConsumeFileUploadNotifyRes;
import com.umpay.dto.merBusi.req.consume.ConsumeFileUploadNotifyBusiReq;
import com.umpay.dto.merBusi.res.consume.ConsumeFileUploadNotifyBusiRes;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.PropertiesUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*******************************************************************
 * description :  外部交易入账文件上传通知 请求业务模块接口处理类
 * @author zhangru
 * @date 2018年4月20日 上午11:01:30
 * @version 1.0
 *******************************************************************/
@Service
public class ConsumeFileUploadNotifyBusinessImpl implements ConsumeFileUploadNotifyBusiness {

	public static final Logger log = LogManager.getLogger(ConsumeFileUploadNotifyBusinessImpl.class);
	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;
	

	@Override
	public Message<ConsumeFileUploadNotifyRes> consumeFileUploadNotify(
	        ConsumeFileUploadNotifyReq req) {
		log.info(String.format("外部交易入账文件上传通知，请求参数为:%s", req));
		
		String merId = StringUtils.trim(req.getMer_id()); //	平台编号
		String notifyUrl = StringUtils.trim(req.getNotify_url()); //	服务器异步通知路径
		String version = StringUtils.trim(req.getVersion()); //	版本号
		String busiDate = StringUtils.trim(req.getBusi_date()); //	业务日期
		String busiId = StringUtils.trim(req.getBusi_id()); //	业务类型
		String batchId = StringUtils.trim(req.getBatch_id()); //	批次号
		String totalAmt = StringUtils.trim(req.getTotal_amt()); //	请求总金额
		String totalNum = StringUtils.trim(req.getTotal_num()); //	请求总条目数
		String fileMd5 = StringUtils.trim(req.getFile_md5()); //	文件MD5值
		String bProductId = PropertiesUtil.getBusiStrValue("bsp.bproductid", ""); //		业务产品号
		String pProductId = PropertiesUtil.getBusiStrValue("bsp.consume.fileNotify.pproductid", ""); //		支付产品号
		String accessType = StringUtils.trim(req.getAccessType());  //接入类型
		String accessName = StringUtils.trim(req.getAccessName());  //接入名称
		String depBankMerId = StringUtils.trim(req.getDepBankMerId());  //存管行

		//组织请求merBusi参数
		ConsumeFileUploadNotifyBusiReq consumeFileUploadNotifyBusiReq = new ConsumeFileUploadNotifyBusiReq();
		consumeFileUploadNotifyBusiReq.setRpid(RpidUtils.getRpid());//通信流水
		consumeFileUploadNotifyBusiReq.setVersion(version);//版本号
		consumeFileUploadNotifyBusiReq.setReqDate(TimeUtil.date8());//请求日期
		consumeFileUploadNotifyBusiReq.setReqTime(TimeUtil.time6());//请求时间
		consumeFileUploadNotifyBusiReq.setMerId(merId); ; //	平台编号
		consumeFileUploadNotifyBusiReq.setNotifyUrl(notifyUrl); //	服务器异步通知路径
		consumeFileUploadNotifyBusiReq.setBusiDate(busiDate); //	业务日期
		consumeFileUploadNotifyBusiReq.setBusiId(busiId); //	业务类型
		consumeFileUploadNotifyBusiReq.setBatchId(batchId); //	批次号
		consumeFileUploadNotifyBusiReq.setTotalAmt(totalAmt); //	请求总金额
		consumeFileUploadNotifyBusiReq.setTotalNum(totalNum); //	请求总条目数
		consumeFileUploadNotifyBusiReq.setFileMd5(fileMd5); //	文件MD5值
		consumeFileUploadNotifyBusiReq.setBproductId(bProductId);	//变长8	业务产品号
		consumeFileUploadNotifyBusiReq.setPproductId(pProductId);	//定长8	产品号
		consumeFileUploadNotifyBusiReq.setAccessType(accessType);
		consumeFileUploadNotifyBusiReq.setAccessName(accessName);
		consumeFileUploadNotifyBusiReq.setDepBankMerId(depBankMerId);
		
		//请求交易模块merBusi
		log.info(String.format("外部交易入账文件上传通知，调用远程服务接口请求参数为:%s", consumeFileUploadNotifyBusiReq));
		Message<ConsumeFileUploadNotifyBusiRes> merBusiRes = this.consumeBusiServerFeignClient.consumeFileUploadNotify(consumeFileUploadNotifyBusiReq);
		log.info(String.format("外部交易入账文件上传通知，调用远程服务接口获取结果为:%s", merBusiRes));

		// 组织接入模块响应结果
		Message<ConsumeFileUploadNotifyRes> resMessage = new Message<ConsumeFileUploadNotifyRes>();
		Meta resMeta = new Meta();
		//判断业务响应结果，不为空
		if (merBusiRes != null) {
			Meta metaData = merBusiRes.getMeta();
			String retCode = metaData.getRet_code();
			String retMsg = metaData.getRet_msg();
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			
			if(MerAccessEC.SUCCESS.equals(retCode)) {//成功
				ConsumeFileUploadNotifyBusiRes resInfo = merBusiRes.getData();
				if(resInfo != null) {
					ConsumeFileUploadNotifyRes merAccessRes = new ConsumeFileUploadNotifyRes();
					merAccessRes.setMer_id(StringUtils.trim(resInfo.getMerId()));//商户号
					merAccessRes.setVersion(StringUtils.trim(resInfo.getVersion()));//版本号
					merAccessRes.setBusi_date(StringUtils.trim(resInfo.getBusiDate()));	//	业务日期
					merAccessRes.setBusi_id(StringUtils.trim(resInfo.getBusiId()));	//	业务类型
					merAccessRes.setBatch_id(StringUtils.trim(resInfo.getBatchId()));	//	批次号
					resMessage.setData(merAccessRes);
				} else {
					resMeta.setRet_code(MerAccessEC.MERBUSI_RESPOND_ERR);
					resMeta.setRet_msg("交易结果不明");
				}
			} 
		
		} else {//响应结果为空，交易超时
			resMeta.setRet_code(MerAccessEC.MERBUSI_RESPOND_ERR);
			resMeta.setRet_msg("交易结果不明");
		}

		resMessage.setMeta(resMeta);
		resMessage.setLinks(null);
		return resMessage;
	}


}
